SSM框架整合(4)—— Spring整合SpringMVC

具体内容请点击阅读全文…

Spring 整合 SpringMVC

结构目录

  • java
    • controller
      • AccountController.java(表现层)
    • dao
      • AccountDao.java(持久层)
    • domain
      • Account.java(JavaBean对象)
    • service
      • AccountService.java(业务层)
      • AccountServiceImp.java(业务层)
  • resources
    • applicationContext.xml(Spring配置文件)
    • springmvc.xml(SpringMVC配置文件)
  • webapp
    • WEB-INF
      • pages
        • list.jsp(jsp页面文件)
      • web.xml(web配置文件)
    • index.jsp(jsp页面文件)

web配置文件

  • 监听器
    • 配置 监听器(ContextLoaderListener)
    • 配置 服务器启动时,加载配置文件(contextConfigLocation)
  • 前端控制器
  • 过滤器

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- 监听器 -->
<!-- 服务器启动时,加载Spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- 前端控制器 -->
<!-- 服务器启动时,DispatcherServlet对象被创建,加载SpringMVC配置文件 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

表现层

  • 通过依赖反转获取 业务层对象,并执行业务层的方法,到达Spring与SpringMVC的整合。

AccountController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.water.controller;

import cn.water.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("accountController")
public class AccountController {

@Autowired
private AccountService service;

@RequestMapping("findAll")
public String findAll(){
/* 表现层 */
System.out.println("表现层:查询所有用户信息");
/* 业务层 */
service.findAll();
return "list";
}

}

执行结果

点击 index.jsp 页面,执行表现层中的findAll(),和业务层的findAll(),最后跳转到 list.jsp 页面。

1
2
表现层:查询所有用户信息
业务层:查询所有用户
-------------本文结束-------------
Donate comment here